<?php

/**
 * This is the model class for table "subjects".
 *
 * The followings are the available columns in table 'subjects':
 * @property integer $subjects_id
 * @property string $subjects_title
 * @property string $subjects_memo
 * @property integer $subjects_tests_id
 * @property string $subjects_create_time
 * @property string $subjects_edit_time
 */
class Subjects extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Subjects the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'subjects';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('subjects_title', 'length', 'max'=>50),
			array('subjects_memo', 'length', 'max'=>255),
			//不能为空
			array('subjects_title,subjects_memo,subjects_tests_id','required'),
			array('subjects_title,subjects_memo', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('subjects_id, subjects_title, subjects_memo, subjects_tests_id, subjects_create_time, subjects_edit_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'subjectsTestsId'=>array(self::BELONGS_TO,'Tests','subjects_tests_id'),
			'testsquestion'=>array(self::HAS_MANY,'TestsQuestion','tests_question_sid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'subjects_id' => '大题管理',
			'subjects_title' => '题目标题',
			'subjects_memo' => '大题说明',
			'subjects_tests_id' => '试题ID',
			'subjects_create_time' => '创建时间',
			'subjects_edit_time' => '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('subjects_id',$this->subjects_id);

		$criteria->compare('subjects_title',$this->subjects_title,true);

		$criteria->compare('subjects_memo',$this->subjects_memo,true);

		$criteria->compare('subjects_tests_id',$this->subjects_tests_id);

		$criteria->compare('subjects_create_time',$this->subjects_create_time,true);

		$criteria->compare('subjects_edit_time',$this->subjects_edit_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave(){
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->subjects_create_time = $this->subjects_edit_time = date("Y-m-d H:i:s");
			}else{
				$this->subjects_edit_time = date("Y-m-d H:i:s");
			}
			return true;
		}else{
			return false;
		}
	}
}